

/*虚拟机模块功能测试*/
#include "../mqserver/mq_virtualhost.hpp"
#include <gtest/gtest.h>

class HostTest : public testing::Test
{
public:
    void SetUp() override
    {
        _host = std::make_shared<xgmq::VirtualHost>("host1", "./data/", "./data/dbfile");

        std::unordered_map<std::string, std::string> empty_map;
        _host->declareExchange("交换机1", xgmq::ExchangeType::DIRECT, true, false, empty_map);
        _host->declareExchange("交换机2", xgmq::ExchangeType::DIRECT, true, false, empty_map);
        _host->declareExchange("交换机3", xgmq::ExchangeType::DIRECT, true, false, empty_map);

        _host->declareQueue("队列1", true, false, false, empty_map);
        _host->declareQueue("队列2", true, false, false, empty_map);
        _host->declareQueue("队列3", true, false, false, empty_map);

        _host->bind("交换机1", "队列1", "北京");
        _host->bind("交换机1", "队列2", "wgs");
        _host->bind("交换机1", "队列3", "西安");

        _host->bind("交换机2", "队列1", "北京");
        _host->bind("交换机2", "队列2", "武功山");
        _host->bind("交换机2", "队列3", "西安");

        _host->bind("交换机3", "队列1", "北京");
        _host->bind("交换机3", "队列2", "武功山");
        _host->bind("交换机3", "队列3", "西安");

        _host->basePublish("队列1", nullptr, "hello-world1");
        _host->basePublish("队列1", nullptr, "hello-world2");
        _host->basePublish("队列1", nullptr, "hello-world3");

        _host->basePublish("队列2", nullptr, "hello-world1");
        _host->basePublish("队列2", nullptr, "hello-world2");
        _host->basePublish("队列2", nullptr, "hello-world3");

        _host->basePublish("队列3", nullptr, "hello-world1");
        _host->basePublish("队列3", nullptr, "hello-world2");
        _host->basePublish("队列3", nullptr, "hello-world3");
    }
    void TearDown() override
    {
        // _host->clear();
    }

public:
    xgmq::VirtualHost::ptr _host;
};

TEST_F(HostTest, init_test)
{
    ASSERT_EQ(_host->existsExchange("交换机1"), true);
    ASSERT_EQ(_host->existsExchange("交换机2"), true);
    ASSERT_EQ(_host->existsExchange("交换机3"), true);

    ASSERT_EQ(_host->existsQueue("队列1"), true);
    ASSERT_EQ(_host->existsQueue("队列2"), true);
    ASSERT_EQ(_host->existsQueue("队列3"), true);

    ASSERT_EQ(_host->existsBind("交换机1", "队列1"), true);
    ASSERT_EQ(_host->existsBind("交换机1", "队列2"), true);
    ASSERT_EQ(_host->existsBind("交换机1", "队列3"), true);

    ASSERT_EQ(_host->existsBind("交换机2", "队列1"), true);
    ASSERT_EQ(_host->existsBind("交换机2", "队列2"), true);
    ASSERT_EQ(_host->existsBind("交换机2", "队列3"), true);

    ASSERT_EQ(_host->existsBind("交换机3", "队列1"), true);
    ASSERT_EQ(_host->existsBind("交换机3", "队列2"), true);
    ASSERT_EQ(_host->existsBind("交换机3", "队列3"), true);

    // 消息存在的测试
    xgmq::MessagePtr msg = _host->basicConsume("队列1");
    ASSERT_EQ(msg->payload().body(), std::string("hello-world1"));

    xgmq::MessagePtr msg2 = _host->basicConsume("队列1");
    ASSERT_EQ(msg2->payload().body(), std::string("hello-world2"));

    xgmq::MessagePtr msg3 = _host->basicConsume("队列1");
    ASSERT_EQ(msg3->payload().body(), std::string("hello-world3"));
}

TEST_F(HostTest, remove_test)
{
    _host->deleteExchange("交换机1");
    ASSERT_EQ(_host->existsExchange("交换机1"), false);
    ASSERT_EQ(_host->existsBind("交换机1", "队列1"), false);
    ASSERT_EQ(_host->existsBind("交换机1", "队列2"), false);
    ASSERT_EQ(_host->existsBind("交换机1", "队列3"), false);

    _host->deleteQueue("队列2");
    ASSERT_EQ(_host->existsBind("交换机1", "队列2"), false);
    ASSERT_EQ(_host->existsBind("交换机2", "队列2"), false);
    ASSERT_EQ(_host->existsBind("交换机3", "队列2"), false);

    xgmq::MessagePtr msg2 = _host->basicConsume("队列2");
    ASSERT_EQ(msg2.get(), nullptr);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return 0;
}